'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 修改联盟信息
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var unionGroupConf = require('../../../../config/unionGroupConf');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionPositionType = require('../../../../consts/unionPositionType');
var PlayerManager = require('../../../../libs/playerManager');
var roleService = require('../../../../services/roleService');
var filterSensitiveService = require('../../../../services/filterSensitiveService');
var constantUtil = require('../../../../util/constantUtil.js');
var roleModel = require('../../../../models/roleModel.js');

/**
 * 修改联盟信息
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var union_announce = msg.union_announce;
    var union_flag_id = msg.union_flag_id;
    var limit_lv = msg.limit_lv;

    if (!union_announce || !union_flag_id || !limit_lv) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    // 是否有敏感詞彙
    if (!filterSensitiveService.detect(union_announce)) {
        return next(null, {
            code: Code.UNION_ANNOUNCE_CONTAIN_SENSITIVE
        });
    }

    var player_manager = new PlayerManager(role_id);
    var union_group_model;
    var union_id;

    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                union_id = role_model.getUnionId();
                if (!!union_id) {
                    unionGroupModel.getById(union_id, function (err, model) {
                        union_group_model = model;
                        cb();
                    });
                } else {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
            },
            function (cb) {

                // 判断否是会长
                if (union_group_model.getUserPosition(role_id) !== 1) {
                    return next(null, {
                        code: Code.UNION_USER_NOT_BOOS
                    });
                }

                // 设置公告
                union_group_model.setUnionAnnounce(union_announce);

                // 设置旗帜
                union_group_model.setUnionFlag(union_flag_id);

                // 设置加入等级
                union_group_model.setUnionJoinLimirLevel(limit_lv);

                cb();
            },
            function (cb) {
                // 同步联盟旗帜到个人角色名
                roleModel.updateUnionFlag(union_id, union_flag_id, cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            union_group_model.save();
            return next(null, {
                code: Code.OK
            });
        }
    );
};
